from datetime import datetime
from app import db


class Matchmaker(db.Model):
    """红娘信息模型"""
    __tablename__ = 'matchmakers'

    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, nullable=False, unique=True, comment='关联管理员ID')
    name = db.Column(db.String(50), nullable=False, comment='红娘姓名')
    phone = db.Column(db.String(20), comment='联系电话')
    email = db.Column(db.String(100), comment='邮箱')
    avatar = db.Column(db.String(255), comment='头像')
    introduction = db.Column(db.Text, comment='个人介绍')
    speciality = db.Column(db.String(200), comment='专长领域')
    status = db.Column(db.Integer, default=1, comment='状态：1在职2离职')
    service_count = db.Column(db.Integer, default=0, comment='服务次数')
    success_count = db.Column(db.Integer, default=0, comment='成功次数')
    rating = db.Column(db.Numeric(3, 2), default=5.00, comment='评分')
    total_commission = db.Column(db.Numeric(10, 2), default=0.00, comment='总佣金')
    available_balance = db.Column(db.Numeric(10, 2), default=0.00, comment='可提现余额')
    frozen_balance = db.Column(db.Numeric(10, 2), default=0.00, comment='冻结余额')
    total_withdraw = db.Column(db.Numeric(10, 2), default=0.00, comment='总提现金额')
    wechat_account = db.Column(db.String(100), comment='微信账号')
    alipay_account = db.Column(db.String(100), comment='支付宝账号')
    bank_name = db.Column(db.String(100), comment='银行名称')
    bank_account = db.Column(db.String(50), comment='银行账号')
    bank_holder = db.Column(db.String(50), comment='开户姓名')
    is_verified = db.Column(db.Boolean, default=False, comment='是否实名认证')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')

    # 索引
    __table_args__ = (
        db.Index('idx_status', 'status'),
        db.Index('idx_phone', 'phone'),
    )

    def __repr__(self):
        return f'<Matchmaker {self.name}>'

    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'admin_id': self.admin_id,
            'name': self.name,
            'phone': self.phone,
            'email': self.email,
            'avatar': self.avatar,
            'introduction': self.introduction,
            'speciality': self.speciality,
            'status': self.status,
            'status_text': '在职' if self.status == 1 else '离职',
            'service_count': self.service_count,
            'success_count': self.success_count,
            'rating': float(self.rating) if self.rating else 5.00,
            'total_commission': float(self.total_commission) if self.total_commission else 0.00,
            'available_balance': float(self.available_balance) if self.available_balance else 0.00,
            'frozen_balance': float(self.frozen_balance) if self.frozen_balance else 0.00,
            'total_withdraw': float(self.total_withdraw) if self.total_withdraw else 0.00,
            'is_verified': self.is_verified,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        } 